package threadPoolTest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @FixedThreadPool: 定长线程池 每当提交一个任务就创建一个线程，直到达到线程池的最大数量，
 *                          这时线程数量不再变化，当线程发生错误结束时，线程池会补充一个新的线程
 * @author: ZBoHang
 * @time: 2021/9/6 17:06
 */
public class FixedThreadPool {

    static ExecutorService fixedExec = Executors.newFixedThreadPool(3);

    public static void main(String[] args) {
        test();
    }

    private static void test() {
        for (int i = 0; i < 7; i++) {
            final int index = i;
            fixedExec.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " index -->" + index);
                    try {
                        Thread.sleep(3000);
                        System.out.println(Thread.currentThread().getName() + " <<-- 3s后 ~~");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }
}
